<template>
  <div class="app-container">
    <div class="filter-container">
      <statisSearchBar :start-date="searchForm.startDate" :end-date="searchForm.endDate" :showp-pf-channel="true" @transferFilter="handleFilter" />
    </div>
    <div ref="echartsContainer" style="width: 100%; height: 400px;"></div>
    <el-table v-loading="listLoading" :data="list" class="mt-15" border>
      <el-table-column label="时间" prop="date" width="100px" align="center"></el-table-column>
      <el-table-column label="总数" prop="total" width="74px" align="center"></el-table-column>
      <el-table-column v-for="(hour, index) in hourList" :key="index" align="center" width="62px" :label="hour+'时'">
        <template slot-scope="scope">
          {{ scope.row.hours[hour] }}
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import StatisSearchBar from '@/components/StatisSearchBar'
import { getRegister } from '@/api/statis'

export default {
  name: 'OnlineList',
  components: { StatisSearchBar },
  data() {
    return {
      list: [],
      hourList: [],
      chartSeriesData: [],
      chartxAxisData: [],
      listLoading: false,
      searchForm: {
        startDate: new Date(Date.now() - 86400000 * 10),
        endDate: new Date()
      }
    }
  },
  mounted() {

  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter(query) {
      this.searchForm = query
      this.getList()
    },
    async getList() {
      this.listLoading = true
      const res = await getRegister(this.searchForm)
      this.listLoading = false
      this.list = res.data.items
      this.hourList = res.data.hours
      this.hanleChart()
    },

    hanleChart() {
      this.chartSeriesData = []
      this.chartxAxisData = []
      this.list.forEach(v => {
        if (this.list.length > 3) {
          this.chartSeriesData.push(v.total)
          this.chartxAxisData.push(v.date)
        } else {
          for (let i = 0; i < this.hourList.length; i++) {
            const h = this.hourList[i]
            let dh = h
            if (h === '00') dh = v.date + ' ' + h
            const d = {}
            d.name = v.date + ' ' + h + '时'
            d.value = v.hours[h]
            this.chartSeriesData.push(d)
            this.chartxAxisData.push(dh)
          }
        }
      })

      var myChart = echarts.init(this.$refs.echartsContainer)
      myChart.setOption(
        {
          tooltip: {
            trigger: 'axis',
            formatter: function(params) {
              var xAxisData = params[0].name
              var seriesName = params[0].seriesName
              var value = params[0].value
              return `${xAxisData}<br/>${seriesName}: ${value}`
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.chartxAxisData
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '注册数',
              type: 'line',
              stack: 'Total',
              smooth: true,
              data: this.chartSeriesData
            }
          ]
        }
      )
    }
  }
}
</script>
